package com.mlog.njyjy.ndop.monitor.collector.dao;

import cn.hutool.core.util.StrUtil;
import com.mlog.njyjy.ndop.monitor.common.domain.CollectWay;
import com.mlog.njyjy.ndop.monitor.common.domain.Task;
import com.mlog.njyjy.ndop.monitor.common.utils.DateTimeUtil;
import com.mlog.njyjy.ndop.share.sql.dao.BaseSqlRepository;
import com.mlog.njyjy.ndop.share.sql.support.SqlTableUtil;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zyz
 * @version v1.0, 2020/07/28
 */
@Repository
public class TaskRepository extends BaseSqlRepository<Task> {

    private CollectWayRepository collectPropertiesRepository;

    public TaskRepository(CollectWayRepository collectPropertiesRepository) {
        this.collectPropertiesRepository = collectPropertiesRepository;
    }

    /**
     * 获取结束时间晚于指定时间的监控任务
     *
     * @param dateTime
     * @return
     */
    public List<Task> findLater(LocalDateTime dateTime) {
        return find(StrUtil.format("select * from {} where end_proc_at >?", SqlTableUtil.getTblName(Task.class)),
                DateTimeUtil.formatDefault(dateTime));
    }

    /**
     * 获取任务单关联的采集方式
     *
     * @param wayId
     * @return
     */
    public CollectWay findWays(int wayId) {
        List<CollectWay> items = collectPropertiesRepository.find(StrUtil.format("select * from {} where id=?", SqlTableUtil.getTblName(CollectWay.class)),
                wayId);
        return items.get(0);
    }
}
